/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/snowy
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/snowy
6.若您的项目无法满足以上几点，可申请商业授权，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.core;

import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson2.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import ink.rayin.tools.utils.ResourceUtil;
//import net.sf.jasperreports.engine.*;
//import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import vip.xiaonuo.SnowyApplication;
import vip.xiaonuo.core.util.PDTCommUtils;

import javax.annotation.Resource;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;


/**
 * 基础测试类
 *
 * @author yubaoshan
 * @date 2017/5/21 16:10
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SnowyApplication.class)
@WebAppConfiguration
//@Transactional(rollbackFor = Exception.class) //打开的话测试之后数据可自动回滚
public class BaseJunit {

    @Resource
    private WebApplicationContext webApplicationContext;

    private MockMvc mockMvc;

/*    *//**
     *
     * @param htmlFile html文件存储路径
     * @param pdfFile  生成的pdf文件存储路径
     * @param chineseFontPath 中文字体存储路径
     */
//    public static void html2pdf(String htmlFile, String pdfFile, String chineseFontPath)  {
//        // step 1
//        String url;
//        OutputStream os = null;
//        try {
//            url = new File(htmlFile).toURI().toURL().toString();
//            os = new FileOutputStream(pdfFile);
//            ITextRenderer renderer = new ITextRenderer();
//            renderer.setDocument(url);
//            // 解决中文不显示问题
//            ITextFontResolver fontResolver = renderer.getFontResolver();
//            fontResolver.addFont(chineseFontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//
//            renderer.layout();
//            renderer.createPDF(os);
//        } catch (MalformedURLException e) {
//            System.err.println(e.toString());
//        } catch (FileNotFoundException e) {
//            System.err.println(e.toString());
//
//        } catch (com.lowagie.text.DocumentException e) {
//            System.err.println(e.toString());
//
//        } catch (IOException e) {
//            System.err.println(e.toString());
//
//        } finally {
//            if(os != null) {
//                try {
//                    os.close();
//                } catch (IOException e) {
//                }
//            }
//        }
//    }

    public static void main(String[] args) throws IOException {
        String outputFileClass = ResourceUtil.getResourceAbsolutePathByClassPath("./");

        // 生成pdf路径
        // generate pdf path
        String outputFile = new File(outputFileClass)
                .getParentFile().getParent()
                + "/tmp/"
                + "example01_openhtmltopdf_"+System.currentTimeMillis() + ".pdf";
       // PDTCommUtils.genderPdf("examples/example1/element1.html", null,outputFile);
        ByteArrayOutputStream outputStream = PDTCommUtils.genderPdf("pdf_tem/tem1/element1.html", jsonObject());
        IOUtils.write(outputStream.toByteArray(), Files.newOutputStream(Paths.get(outputFile)));
        outputStream.flush();
        outputStream.close();
    }

    private static JSONObject jsonObject(){
                HashMap<String, Object> objectObjectHashMap = Maps.newHashMap();
        ArrayList<Object> objects = Lists.newArrayList();
        objectObjectHashMap.put("title","魏氏族谱");
        objectObjectHashMap.put("time", "123");
        objectObjectHashMap.put("othoer","张三");
        for (int i = 0; i < 5; i++) {
            objects.add(MapUtil.builder().put("name","name"+i).put("title","title"+i).put("desc","desc"+i)
                    .put("mark","字恭士生庚未详"+i)
                    .put("xifu","娶周氏"+i)
                    .put("sheng1","生庚未详公殁葬"+i)
                    .put("sheng2","龟肩艮山坤向妣"+i)
                    .put("sheng3","殁葬伴公同应同向"+i)
                    .put("sheng4","生子四"+i)
                    .put("sheng5","敬元"+i)
                    .put("sheng6","敬元"+i)
                    .put("sheng7","敬元"+i)
                    .put("sheng8","敬元"+i)
                    .build());
        }
        objectObjectHashMap.put("topDeviceList",objects);
        return JSONObject.parseObject(JSONObject.toJSONString(objectObjectHashMap));
    }

//    @Test
//    public void test1() throws Exception {
//        HashMap<String, Object> objectObjectHashMap = Maps.newHashMap();
//        ArrayList<Object> objects = Lists.newArrayList();
//
//        for (int i = 0; i < 10; i++) {
//            objects.add(MapUtil.builder().put("name","name"+i).put("title","title"+i).put("desc","desc"+i)
//                    .put("mark","字恭士生庚未详"+i)
//                    .put("xifu","娶周氏"+i)
//                    .put("sheng1","生庚未详公殁葬"+i)
//                    .put("sheng2","龟肩艮山坤向妣"+i)
//                    .put("sheng3","殁葬伴公同应同向"+i)
//                    .put("sheng4","生子四"+i)
//                    .put("sheng5","敬元"+i)
//                    .put("sheng6","敬元"+i)
//                    .put("sheng7","敬元"+i)
//                    .put("sheng8","敬元"+i)
//                    .build());
//        }
//        objectObjectHashMap.put("topDeviceList",objects);
//        HtmlUtils.html2Pdf(HtmlUtils.getTemplateContent("dem2.ftl", objectObjectHashMap),"E:\\codes\\xunye_codes\\genealogy\\genealogy_apis\\hello.pdf");
//    }

//    @SneakyThrows
//    @Test
//    public void test2() throws IOException {
//        ClassPathResource resource = new ClassPathResource("templates/jasper_pdf_1.jasper");
//        ArrayList<LogyForm> objects = Lists.newArrayList();
//
//        HashMap<String, Object> objectObjectHashMap1 = new HashMap<>();
//        objectObjectHashMap1.put("title","家谱");
//        objectObjectHashMap1.put("summary","fuwen");
//
//        for (int i = 0; i < 10; i++) {
//            LogyForm logyForm = new LogyForm();
//            logyForm.setLogyName("第\r"+i+"\r世");
//            objects.add(logyForm);
//        }
//
//        JasperPrint jasperPrint = JasperFillManager.fillReport(resource.getInputStream(), objectObjectHashMap1,new JRBeanCollectionDataSource(objects));
//        JasperRunManager.runReportToPdfStream(resource.getInputStream(),new FileOutputStream("E:\\codes\\xunye_codes\\genealogy\\genealogy_apis\\hello.pdf"),objectObjectHashMap1,new JRBeanCollectionDataSource(objects));
//      //  JasperExportManager.exportReportToPdfStream(jasperPrint,new FileOutputStream("E:\\codes\\xunye_codes\\genealogy\\genealogy_apis\\hello.pdf"));
//    }

    @Before
    public void setupMockMvc() {
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }

    @Before
    public void initDatabase() {
    }


}
